class CommitmentController {

    // the delete, save and update actions only accept POST requests
	def allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']

    def commitmentService
    def authenticateService

    def list = {

        def userDomain = authenticateService.userDomain()
        if (!userDomain) {
            redirect(controller: "login")
        }
        int loggedUserId = userDomain.id
        def loggedUser = User.get(loggedUserId)

        def person = User.get(params.id)

		if (!person) {
			flash.message = "User not found with id ${params.id}"
			redirect(uri:"/")
			return
		}

        /* Tylko właściciel konta i administrator mogą oglądać historię konta */
        if ((loggedUser.id != person.id) && !loggedUser.isAdmin()) {
            flash.message = "Access deny!"
            redirect(uri:"/")
            return
        }

        params.max = params.max ?: 10
        params.sort = params.sort ?: "dateCreated"
        params.order = params.order ?: "desc"

        def commitments = Commitment.findAllByUser(person, params)

		[person: person, commitments: commitments]
    }

    def edit = {
        def commitment = Commitment.get(params.id)

        [commitment: commitment]
    }

    def update = {
        def commitment = Commitment.get(params.id)
        def user = commitment.user
        if (commitment) {
            commitment.value = params.value.toInteger()
            commitment.description = params.description

            def oldCommitmentType = commitment.commitmentType
            def commitmentType = CommitmentType.get(params.commitmentTypeId)

            if (commitmentType) {
                oldCommitmentType.removeFromCommitments(commitment)
                commitment.commitmentType = commitmentType
            }
            else {
                flash.message = "CommitmentType not found with id ${params.commitmentType}"
            }

            commitmentService.recomputeBalance(user)
        }
        else {
            flash.message = "Commitment not found with id ${params.id}"
        }
        redirect(action: 'list', id: user.id)
    }

    def delete = {
        def commitment = Commitment.get(params.id)
        def user = commitment.user
        if (commitment) {
            user.removeFromCommitments(commitment)
            commitment.delete()

            commitmentService.recomputeBalance(user)

            flash.message = "Commitment ${params.id} deleted."
        }
        else {
            flash.message = "Commitment not found with id ${params.id}"
        }
        redirect(action: 'list', id: user.id)
    }

    def create = {
        def person = User.get(params.id)
		if (!person) {
			flash.message = "User not found with id ${params.id}"
			redirect(controller: user, action: list)
			return
		}

        def commitment = new Commitment()

		[person: person, commitment: commitment]
    }

    def save ={
        def person = User.get(params.personid)
		if (!person) {
			flash.message = "User not found with id ${params.personid}"
			redirect(controller: 'user', action: 'list')
			return
		}

        def commitmentType = CommitmentType.get(params.commitmentTypeId)
        if (!commitmentType) {
            flash.message = "CommitmentType not found with id ${params.commitmentTypeId}"
            render(controller: 'user', view: 'editCommitments', model: [person: person])
            return
        }

        def commitment = new Commitment()
        commitment.value = params.value.toInteger()
        commitment.description = params.description
        commitment.user = person
        commitment.commitmentType = commitmentType

        commitmentService.computeBalance(commitment)//obliczamy saldo po operacji

        if (commitment.save()) {
            person.addToCommitments(commitment)
            commitmentType.addToCommitments(commitment)
            redirect(action: 'list', id: person.id)
        }
        else {
            render(view: 'create', model:[person: person, commitment: commitment])
        }
    }

    def balance = {
        def loggedUser = authenticateService.userDomain()

        def person = User.get(loggedUser.id)

        if (!person) {
            render '<p>Nie można zweryfikować stanu konta.</p>'
            return
        }

        render(template: "balance", model: [person: person])
    }
}
